package org.wangbao.service;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.wangbao.model.News;

import com.google.gson.Gson;

public class VersionControlService {

	List<News> resultList;
	HtmlParserUtil hpu;
	ContentParserService contentParserService;
	
	Map valueMap;
	
	

	
	
	
	/**
	 * 根据期刊号和版面号获得图片的访问路径
	 * @param pic
	 */
	public static String getPicPath(String jouId,String verId,String pic){
		
		//先固定层次
//		Map xMap = (Map)valueMap.get(jouId);
//		Map yMap = (Map)xMap.get(verId);
//		yMap.get(pic);
		
		
		String tmpPath = "ds/"+jouId+"/"+verId+"/"+pic; 
		//System.out.println(tmpPath);
		return tmpPath;
		
	}
	
	
	
	

	/**
	 * 得到期刊和版号目录
	 * 
	 * @param url
	 * @param keyword
	 * @return
	 * @throws Exception
	 */
	@Test
	public String handleJournalList() throws Exception {
		
		//String rootpath = "D:\\MyWorkSpace\\wangbao\\resource\\ds";
		//此目录由resource下移到 web工程的根目录下
		String rootpath = contentParserService.handleWebPath()+"ds";
		valueMap = new HashMap();
		File root = new File(rootpath);
		handleFileList(root,valueMap);

		Gson gson = new Gson();
		String newsToJson = gson.toJson(valueMap);
//		System.out.println(newsToJson);
		return newsToJson;
	}

	
	
	public void handleFileList(File f,Map map) throws Exception {
		
			String[] arr = f.list();

			//需要对字符串数组排序
			//Arrays.sort(arr);
			
			for (int i=0;i<arr.length;i++){
				File xf =new File(f.getAbsolutePath()+"/"+arr[i]);
				if (xf.isDirectory()){
					Map xmap = new HashMap();
					map.put(arr[i], xmap);
					handleFileList(xf,xmap);
				}
				else
				{
					//根据要求只传回图片名称
					if (xf.getName().contains(".jpg"))
						map.put(arr[i],arr[i]);
				}
			}
	}

	public ContentParserService getContentParserService() {
		return contentParserService;
	}

	public void setContentParserService(ContentParserService contentParserService) {
		this.contentParserService = contentParserService;
	}


}
